{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# pandas的函数应用",
   "id": "def06b2bd23eda03"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:18:34.774570Z",
     "start_time": "2025-01-08T06:18:34.293621Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# Numpy ufunc 函数，randn跟的是维数\n",
    "df = pd.DataFrame(np.random.randn(5,4) - 1)\n",
    "print(df)\n",
    "\n",
    "print(np.abs(df)) #绝对值"
   ],
   "id": "27c4de16f28a8494",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0 -0.622941  0.011810 -1.526016 -0.550830\n",
      "1 -0.739007 -2.242057  0.025188 -1.077614\n",
      "2 -0.181066 -3.323488 -2.307261 -1.121668\n",
      "3 -1.425311 -2.176152  1.121475 -2.036460\n",
      "4 -0.890302 -0.536005 -0.233667 -0.262244\n",
      "          0         1         2         3\n",
      "0  0.622941  0.011810  1.526016  0.550830\n",
      "1  0.739007  2.242057  0.025188  1.077614\n",
      "2  0.181066  3.323488  2.307261  1.121668\n",
      "3  1.425311  2.176152  1.121475  2.036460\n",
      "4  0.890302  0.536005  0.233667  0.262244\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:20:24.090680Z",
     "start_time": "2025-01-08T06:20:24.085462Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#apply默认作用在列上,x是每一列,因为axis=0\n",
    "# df.apply() 是 pandas 中用于在 DataFrame 或 Series 上应用函数的一个非常有用的方法。它可以沿着指定的轴（行或列）应用一个函数，从而对数据进行转换、计算、处理等操作。\n",
    "print(df.apply(lambda x : x.max()))"
   ],
   "id": "987d9504729c56b3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -0.181066\n",
      "1    0.011810\n",
      "2    1.121475\n",
      "3   -0.262244\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:24:22.738145Z",
     "start_time": "2025-01-08T06:24:22.732842Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#apply作用在行上\n",
    "print(df.apply(lambda x : x.max(), axis=1))"
   ],
   "id": "5a6829a369d255ef",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.011810\n",
      "1    0.025188\n",
      "2   -0.181066\n",
      "3    1.121475\n",
      "4   -0.233667\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:29:28.697241Z",
     "start_time": "2025-01-08T06:29:28.682871Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用applymap应用到每个数据\n",
    "print(df.map(lambda x : '%.2f' % x))\n",
    "df.dtypes"
   ],
   "id": "a46857e7857a092a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2      3\n",
      "0  -0.62   0.01  -1.53  -0.55\n",
      "1  -0.74  -2.24   0.03  -1.08\n",
      "2  -0.18  -3.32  -2.31  -1.12\n",
      "3  -1.43  -2.18   1.12  -2.04\n",
      "4  -0.89  -0.54  -0.23  -0.26\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0    float64\n",
       "1    float64\n",
       "2    float64\n",
       "3    float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 索引排序（不重要）",
   "id": "4a4a245d13d11e9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:31:58.802131Z",
     "start_time": "2025-01-08T06:31:58.791286Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Series\n",
    "print(np.random.randint(5, size=5))\n",
    "print('-'*50)\n",
    "s4 = pd.Series(range(10, 15), index = np.random.randint(5, size=5)) #索引随机生成\n",
    "print(s4)\n",
    "print('-'*50)\n",
    "# 索引排序,sort_index返回一个新的排好索引的series\n",
    "print(s4.sort_index())\n",
    "print(s4)\n",
    "# s4.loc[0:3]  loc索引值不唯一时直接报错\n",
    "print(s4.iloc[0:3])\n",
    "s4[0:3]  #默认用的位置索引"
   ],
   "id": "65f01e326e292a0c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3 3 1 2]\n",
      "--------------------------------------------------\n",
      "2    10\n",
      "2    11\n",
      "3    12\n",
      "4    13\n",
      "3    14\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "2    10\n",
      "2    11\n",
      "3    12\n",
      "3    14\n",
      "4    13\n",
      "dtype: int64\n",
      "2    10\n",
      "2    11\n",
      "3    12\n",
      "4    13\n",
      "3    14\n",
      "dtype: int64\n",
      "2    10\n",
      "2    11\n",
      "3    12\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2    10\n",
       "2    11\n",
       "3    12\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "# s4.loc[1:2] #loc索引值唯一时可以切片",
   "id": "87f2ab1f2a1914ec"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:33:51.194096Z",
     "start_time": "2025-01-08T06:33:51.184062Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame\n",
    "df4 = pd.DataFrame(np.random.randn(5, 5),\n",
    "                   index=np.random.randint(5, size=5),\n",
    "                   columns=np.random.randint(5, size=5))\n",
    "print(df4)\n",
    "#轴零是行索引排序\n",
    "df4_isort = df4.sort_index(axis=0, ascending=False)\n",
    "print(df4_isort)"
   ],
   "id": "8845debc5a0b3e3f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          4         0         1         2         0\n",
      "0  1.442937  0.215825 -0.245501 -1.370137 -0.122420\n",
      "4 -1.381834  0.205983  0.476378 -0.013343 -2.176303\n",
      "1 -0.308491 -0.716015 -0.863050 -0.970908 -0.662908\n",
      "1 -0.810281 -1.356547 -0.576007  0.815795 -1.644265\n",
      "1 -1.900868 -0.518982 -1.586868  0.353500  0.856867\n",
      "          4         0         1         2         0\n",
      "4 -1.381834  0.205983  0.476378 -0.013343 -2.176303\n",
      "1 -0.810281 -1.356547 -0.576007  0.815795 -1.644265\n",
      "1 -0.308491 -0.716015 -0.863050 -0.970908 -0.662908\n",
      "1 -1.900868 -0.518982 -1.586868  0.353500  0.856867\n",
      "0  1.442937  0.215825 -0.245501 -1.370137 -0.122420\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:34:58.527244Z",
     "start_time": "2025-01-08T06:34:58.520598Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#轴1是列索引排序\n",
    "df4_isort = df4.sort_index(axis=1, ascending=True)\n",
    "print(df4_isort)"
   ],
   "id": "63b5b1855969974d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         0         1         2         4\n",
      "0  0.215825 -0.122420 -0.245501 -1.370137  1.442937\n",
      "4  0.205983 -2.176303  0.476378 -0.013343 -1.381834\n",
      "1 -0.716015 -0.662908 -0.863050 -0.970908 -0.308491\n",
      "1 -1.356547 -1.644265 -0.576007  0.815795 -0.810281\n",
      "1 -0.518982  0.856867 -1.586868  0.353500 -1.900868\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 按值排序（机器学习，深度学习不重要，数据分析才需要）",
   "id": "a72c9f015729041f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:35:33.407334Z",
     "start_time": "2025-01-08T06:35:33.399586Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 按值排序,by后是column的值\n",
    "import random\n",
    "l=[random.randint(0,100) for i in range(24)] #生成24个随机数\n",
    "df4 = pd.DataFrame(np.array(l).reshape(6,4)) #生成6行4列的dataframe\n",
    "# print(df4) #查看数据,ndarray\n",
    "# print('-'*50)\n",
    "print(df4)\n",
    "print('-'*50)\n",
    "#按轴零排序，by后是列名,交换的是行\n",
    "df4_vsort = df4.sort_values(by=3,axis=0, ascending=False) #寻找的是columns里的3,重要\n",
    "print(df4_vsort)\n"
   ],
   "id": "1824473543f89a65",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    0   1   2   3\n",
      "0  79  75  79  48\n",
      "1  93  68  60  63\n",
      "2  92  53  65  53\n",
      "3  66  11  48  57\n",
      "4  62  85  83  48\n",
      "5  53  49  19  11\n",
      "--------------------------------------------------\n",
      "    0   1   2   3\n",
      "1  93  68  60  63\n",
      "3  66  11  48  57\n",
      "2  92  53  65  53\n",
      "0  79  75  79  48\n",
      "4  62  85  83  48\n",
      "5  53  49  19  11\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:37:44.435025Z",
     "start_time": "2025-01-08T06:37:44.429663Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#按轴1排序，by后行索引名，交换的是列\n",
    "df4_vsort = df4.sort_values(by=3,axis=1, ascending=False) #寻找的是index里的3\n",
    "print(df4_vsort)"
   ],
   "id": "1a02653a217e988a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    0   3   2   1\n",
      "0  79  48  79  75\n",
      "1  93  63  60  68\n",
      "2  92  53  65  53\n",
      "3  66  57  48  11\n",
      "4  62  48  83  85\n",
      "5  53  11  19  49\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 处理缺失数据（重要）",
   "id": "4be4426aebdb5001"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:38:56.325879Z",
     "start_time": "2025-01-08T06:38:56.319168Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_data = pd.DataFrame([np.random.randn(3), [1., 2., np.nan],\n",
    "                       [np.nan, 4., np.nan], [1., 2., 3.]])\n",
    "print(df_data.head())"
   ],
   "id": "aab3a3246bc159cd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0 -1.117998 -0.158392 -0.657618\n",
      "1  1.000000  2.000000       NaN\n",
      "2       NaN  4.000000       NaN\n",
      "3  1.000000  2.000000  3.000000\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "np.random.rand()：生成 [0, 1) 区间的均匀分布随机数。\n",
    "np.random.randint()：生成指定范围内的整数随机数。\n",
    "注意区别"
   ],
   "id": "2339b19577d489dc"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:49:20.836327Z",
     "start_time": "2025-01-08T06:49:20.830912Z"
    }
   },
   "cell_type": "code",
   "source": "df_data.iloc[2,0]",
   "id": "bab18f7be897fecd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(nan)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:49:43.616340Z",
     "start_time": "2025-01-08T06:49:43.612013Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#isnull来判断是否有空的数据\n",
    "print(df_data.isnull())"
   ],
   "id": "4d05092c04deddd9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2\n",
      "0  False  False  False\n",
      "1  False  False   True\n",
      "2   True  False   True\n",
      "3  False  False  False\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:50:31.376240Z",
     "start_time": "2025-01-08T06:50:31.370360Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#帮我计算df_data缺失率\n",
    "print(df_data.isnull().sum()/len(df_data))"
   ],
   "id": "1d8846e626600223",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.25\n",
      "1    0.00\n",
      "2    0.50\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 删除缺失数据",
   "id": "ecd0a4a73d2f03f7"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:50:51.625262Z",
     "start_time": "2025-01-08T06:50:51.619230Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#默认一个样本，任何一个特征缺失，就删除\n",
    "#inplace True是修改的是原有的df\n",
    "#subset=[0]是指按第一列来删除,第一列有空值就删除对应的行\n",
    "print(df_data.dropna(subset=[0]))#如果不写subset，则会把所有带有nan的行都删除\n",
    "# df_data"
   ],
   "id": "3755fec7d3db5900",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0 -1.117998 -0.158392 -0.657618\n",
      "1  1.000000  2.000000       NaN\n",
      "3  1.000000  2.000000  3.000000\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:52:44.018458Z",
     "start_time": "2025-01-08T06:52:44.010900Z"
    }
   },
   "cell_type": "code",
   "source": "df_data  #进行删除操作，原有的df_data不变",
   "id": "64067a6e35da4ef0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          0         1         2\n",
       "0 -1.117998 -0.158392 -0.657618\n",
       "1  1.000000  2.000000       NaN\n",
       "2       NaN  4.000000       NaN\n",
       "3  1.000000  2.000000  3.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.117998</td>\n",
       "      <td>-0.158392</td>\n",
       "      <td>-0.657618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:16:01.301927Z",
     "start_time": "2025-01-08T07:16:01.295878Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#用的不多，用在某个特征缺失太多时，才会进行删除\n",
    "print(df_data.dropna(axis=1))  #某列由nan就删除该列"
   ],
   "id": "7e44a2936fef5c76",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          1\n",
      "0 -0.158392\n",
      "1  2.000000\n",
      "2  4.000000\n",
      "3  2.000000\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:16:11.873034Z",
     "start_time": "2025-01-08T07:16:11.866230Z"
    }
   },
   "cell_type": "code",
   "source": "df_data",
   "id": "3c18ae1ed14b6dd5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          0         1         2\n",
       "0 -1.117998 -0.158392 -0.657618\n",
       "1  1.000000  2.000000       NaN\n",
       "2       NaN  4.000000       NaN\n",
       "3  1.000000  2.000000  3.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.117998</td>\n",
       "      <td>-0.158392</td>\n",
       "      <td>-0.657618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 填充缺失数据",
   "id": "ebe0b1f76e552716"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "#均值，中位数，众数填充",
   "id": "fca691cdf25e38a2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:20:30.073640Z",
     "start_time": "2025-01-08T07:20:30.067876Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#依次拿到每一列\n",
    "for i in df_data.columns:\n",
    "    print(df_data.loc[:,i])"
   ],
   "id": "6b75b27f648f239d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -1.117998\n",
      "1    1.000000\n",
      "2         NaN\n",
      "3    1.000000\n",
      "Name: 0, dtype: float64\n",
      "0   -0.158392\n",
      "1    2.000000\n",
      "2    4.000000\n",
      "3    2.000000\n",
      "Name: 1, dtype: float64\n",
      "0   -0.657618\n",
      "1         NaN\n",
      "2         NaN\n",
      "3    3.000000\n",
      "Name: 2, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:18:35.087629Z",
     "start_time": "2025-01-08T07:18:35.079837Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#给零列的空值填为-100，按特征（按列）去填充\n",
    "print(df_data.iloc[:,0].fillna(-100.))\n",
    "df_data  #原来的df_data没有修改,如果要让原来的数组改变，需要加一个参数inplace=True"
   ],
   "id": "458d878014c6bc65",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     -1.117998\n",
      "1      1.000000\n",
      "2   -100.000000\n",
      "3      1.000000\n",
      "Name: 0, dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "          0         1         2\n",
       "0 -1.117998 -0.158392 -0.657618\n",
       "1  1.000000  2.000000       NaN\n",
       "2       NaN  4.000000       NaN\n",
       "3  1.000000  2.000000  3.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.117998</td>\n",
       "      <td>-0.158392</td>\n",
       "      <td>-0.657618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:29:46.597449Z",
     "start_time": "2025-01-08T07:29:46.592066Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# df_data.iloc[:,0].fillna(-100.,inplace=True) #inplace=True后面会被删除\n",
    "df_data.iloc[:,:]=df_data.iloc[:,:].fillna(-100)"
   ],
   "id": "72bb7ffec0af36d2",
   "outputs": [],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:29:50.232802Z",
     "start_time": "2025-01-08T07:29:50.224851Z"
    }
   },
   "cell_type": "code",
   "source": "df_data",
   "id": "52deaadb92318fed",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            0         1           2\n",
       "0   -1.117998 -0.158392   -0.657618\n",
       "1    1.000000  2.000000 -100.000000\n",
       "2 -100.000000  4.000000 -100.000000\n",
       "3    1.000000  2.000000    3.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.117998</td>\n",
       "      <td>-0.158392</td>\n",
       "      <td>-0.657618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>-100.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-100.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>-100.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:30:12.059931Z",
     "start_time": "2025-01-08T07:30:12.056159Z"
    }
   },
   "cell_type": "code",
   "source": "df_data.iloc[:,2]=df_data.iloc[:,2].fillna(df_data.iloc[:,2].mean()) #用均值填充空值",
   "id": "e01d12c5f36fdb72",
   "outputs": [],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:30:27.249851Z",
     "start_time": "2025-01-08T07:30:27.243033Z"
    }
   },
   "cell_type": "code",
   "source": "df_data",
   "id": "ebfd6a4177a086d3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            0         1           2\n",
       "0   -1.117998 -0.158392   -0.657618\n",
       "1    1.000000  2.000000 -100.000000\n",
       "2 -100.000000  4.000000 -100.000000\n",
       "3    1.000000  2.000000    3.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.117998</td>\n",
       "      <td>-0.158392</td>\n",
       "      <td>-0.657618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>-100.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-100.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>-100.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 29
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
